<?php
class OrderAction extends PublicAction{
	public function index(){
		$uid = session('uid');
		$pid = I('pid');
		//session('back');
		$this->checkuid($uid,U('Products/detail',array('pid'=>$pid)));
		
		$productsDetail = D('Products')->getProductsBasic(array('products_id'=>$pid),'products_name,products_price,products_image,products_paimai_price,products_type,products_date_available');
		/*  echo "<pre>";
		var_dump($productsDetail);die; */
		$onlinepay = C('SITE_INFO');//是否支持线上支付
		//var_dump($onlinepay);die;
		$this->onlinepay = $onlinepay['onlinepay']?$onlinepay['onlinepay']:0;
		$this->productsDetail = $productsDetail;
		$this->pid = $pid;
		$this->msg = I('msg',0);
		$this->display();
	}
	//查看订单
	public function detail(){
		$orderid = I('orderid');
		$back = I('back')?'Membership/ordermanage':'Index/index';
		$uid = session('uid');
		$this->checkuid($uid,U('Order/detail',array('back'=>$back,'orderid'=>$orderid)));
		$orderDetail = D('Order')->Orderdetail(array('order_id'=>$orderid));
		/*  echo "<pre>";
		var_dump($orderDetail);die;   */
		$this->orderid = $orderid;
		
		$this->back = U($back);
		//echo  $this->back;die;
		$this->orderDetail = $orderDetail;
		$this->display();
	}
	
	//线上交易
	public function online(){
		$uid = session('uid');
		$products_id = I('post.products_id');
		$this->checkuid($uid,U('Order/online',array('products_id'=>$products_id)));
		//$products_id = I('post.products_id');
		$comment = I('post.comment');
		$data['show_flag']=1;
		//$data['products_id'] = $products_id;
		//$data['close_reason']=3;
		$products = M('products')->where(array('products_id'=>$products_id,'show_flag'=>0))->find();
		if(!$products['products_id']){
			$this->redirect('Order/index',array('pid'=>$products_id,'msg'=>'很抱歉，该商品已经售出'));
		}
		$result = M('products')->where(array('products_id'=>$products_id))->save($data);
		if($result){
			$member = M('membership')->where(array('uid'=>$uid))->find();
			$data1['order_num'] = 'SP'.time();
			$data1['order_type'] = 0;
			$data1['uid']=$uid;
			$data1['publish_uid'] = $products['uid'];
			$data1['tel']=$member['tel'];
			$data1['payment_name'] = $member['name'];
			$data1['comment'] = $comment;
			$data1['order_status_id'] = 1;
			$data1['order_date_added'] = date('Y-m-d H:i:s',time());
			$data1['products_id'] = $products_id;
			$data1['pay_price']  = $products['products_price'];//一口价价格，后期扩展拍卖价格
			$orderid = M('order')->add($data1);
			if($orderid){
				$this->redirect('Order/selectaddress',array('orderid'=>$orderid));			
				
			}else{
				$this->redirect('Order/index',array('pid'=>$products_id,'msg'=>'下单失败，请联系管理人员！'));
			}
		}
	}
	
	//在线支付选择地址
	function selectaddress(){
		$uid = session('uid');
		$orderid = I('orderid');
		$this->checkuid($uid,U('Order/selectaddress',array('orderid'=>$orderid)));
		$orderid = I('orderid');
		if(!$orderid){
			$this->redirect('Products/index');
		}
		//var_dump($_POST);die;
		if($_POST){
			$address_id = I('post.address_id');
			//设置默认收货地址
			M('address')->where(array('uid'=>$uid))->setField('default_flag',0);
			M('address')->where(array('address_id'=>$address_id))->setField('default_flag',1);
			//查询地址信息
			$address = M('address')->where(array('address_id'=>$address_id))->find();
			//var_dump($address);die;
			$data1['order_id'] = $orderid;
			$data1['shipping_lastname']= $address['lastname'];
			$data1['tel'] = $address['phone'];
			$data1['shipping_address_1'] = $address['address_1'];
			$data1['shipping_address_2'] = $address['address_2'];
			$data1['provcode']=$address['provcode'];
			$data1['citycode'] = $address['citycode'];
			$data1['area_id'] =$address['area_id'];			
			$data1['order_date_added'] = date('Y-m-d H:i:s',time());
			$result = M('order')->save($data1);
			if($result){
				$this->redirect('Order/payment',array('orderid'=>$orderid));
			}else{
				$this->msg = '选择地址失败，请联系管理人员';
				$address = M('address')->where(array('uid'=>$uid))->order('address_id desc')->select();
				$this->address = $address;
				$this->orderid = $orderid;
				$this->display();
			}
			
		}else{
			$address = M('address')->where(array('uid'=>$uid))->order('address_id desc')->select();//D('Address')->getAddress(array('uid'=>$uid));//
			if(!$address){
				//$back = U('Order/selectaddress',array('orderid'=>$orderid));
				$this->redirect('Membership/address',array('back'=>'selectaddress','orderid'=>$orderid));
			}
			
			//var_dump($address);die;
			$this->address = $address;
			$this->orderid = $orderid;
			$this->msg = 0;
			$this->display();
		}
	}
	
	//在线支付界面
	public function payment(){
		$uid = session('uid');
		$orderid = I('orderid');
		$this->checkuid($uid,U('Order/payment',array('orderid'=>$orderid)));
		
		$msg = 0;
		if(!$orderid){
			$this->redirect('Products/index');
		}
		$order=M('order')->where(array('order_id'=>$orderid))->find();
				
		$this->msg = $msg?$msg:0;
		$this->payprice = $order['pay_price'];
		$this->orderid = $orderid;
		$this->display();
		
	}
	
	//支付成功确认界面
	public function sucessconfirm(){
		$uid = session('uid');
		$orderid = I('orderid');
		$this->checkuid($uid,U('Order/sucessconfirm',array('orderid'=>$orderid)));
		
		M('order')->where(array('order_id'=>$orderid))->setField('order_status_id',2);
		//echo $orderid;die;
		$order = M('order')->table('`order` o')->join('products p ON o.products_id=p.products_id')->where(array('order_id'=>$orderid))->field('o.*,p.contact_person,p.contact_tel')->find();
		
		//支付成功保存收款人金额变动
		$flag = 1;//处理支付返回参数
		if($flag){
			//获取现有冻结金额
			$member_moneylog = M('member_moneylog')->where(array('uid'=>$order['publish_uid']))->field('active_money,freeze_money')->order('member_moneylog desc')->limit(1)->find();
			$data['uid']=$order['publish_uid'];
			$data['money_type']=1;
			$data['affect_money']=$order['pay_price'];
			$data['active_money']=$member_moneylog['active_money'];
			$data['freeze_money'] = $member_moneylog['freeze_money']+$order['pay_price'];
			$data['moneylog_affect_date'] = date('Y-m-d H:i:s',time());
			$data['order_id'] = $orderid;
			$data['u_ip']=getip();
			$result = M('member_moneylog')->add($data);
			if($result){
				$msg = '支付成功';
			}else{
				$msg = '支付成功，交易失败，联系管理人员！';
			}
		}else{
			$msg = '支付失败';
		}
		
		$this->msg = $msg?$msg:0;
		//echo M('order')->getLastSql();die;
		//$publishmsg = M('membership')->table('membership m')->join('products p ON m.uid=p.uid')->where(array('p.products_id'=>$order['products_id']))->field('m.name,m.tel')->find();
		$this->order = $order;
		//$this->publishmsg = $publishmsg;
		$this->orderid = $orderid;
		$this->display();
		
	}
	//线下交易
	public function line(){
		$uid = session('uid');
		$products_id = I('post.products_id');
		$comment = I('post.comment');
		$this->checkuid($uid,U('Order/line',array('products_id'=>$products_id,'comment'=>$comment)));
		
		
		$data['show_flag']=1;
		//$data['close_reason']=3;
		$products = M('products')->where(array('products'=>$products_id,'show_flag'=>0))->find();
		if(!$products['products_id']){
			$this->redirect('Order/index',array('pid'=>$products_id,'msg'=>'很抱歉，该商品已经售出'));
		}		
		$result = M('products')->where(array('products_id'=>$products_id))->save($data);
		if($result){
			$member = M('membership')->where(array('uid'=>$uid))->find();
			$data1['order_num'] = 'SP'.time();
			$data1['order_type'] = 1;
			$data1['uid']=$uid;
			$data1['products_id'] = $products_id;
			$data1['publish_uid'] = $products['uid'];
			$data1['tel']=$member['tel'];
			$data1['payment_name'] = $member['name'];
			$data1['comment'] = $comment;
			$data1['order_status_id'] = 1;
			$data1['order_date_added'] = date('Y-m-d H:i:s',time());
			$data1['pay_price'] = $products['products_price'];
			//var_dump($data1);die;
			$result = M('order')->add($data1);
			if($result){
				$this->order_num = $data1['order_num'];
				//获取卖家信息
				$this->products = $products;
				$this->orderid = $result;
				$this->display();
			}else{
				$this->redirect('Order/index',array('pid'=>$products_id,'msg'=>'下单失败，请联系管理人员！'));
			}
		}
	}
	
	//订单确认
	function confirmOrder(){
		$uid = session('uid');
		$orderid = I('orderid');
		$this->checkuid($uid,U('Order/confirmOrder',array('orderid'=>$orderid)));
	
		if(!$orderid){
			$msg = '订单操作错误，请联系管理人员';
		}
		
		$data['order_status_id'] = 3;
		$result = M('order')->where(array('order_id'=>$orderid))->save($data);
		if($result){
			$msg='确认订单成功';
		}else{
			$msg = '确认订单失败，联系管理人员';
		}
		$this->redirect('Membership/ordermanage',array('ostatusid'=>3,'bsflag'=>2,'msg'=>$msg));
	}
	//并发货
	function sendOrder(){
		$uid = session('uid');
		$orderid = I('orderid');
		$this->checkuid($uid,U('Order/confirmOrder',array('orderid'=>$orderid)));
		
		if(!$orderid){
			$msg = '订单操作错误，请联系管理人员';
		}
		$data['delivery_company'] = I('post.deliveryCompany');
		$data['delivery_num'] = I('post.deliveryNum');
		$data['order_status_id'] = 5;
		$result = M('order')->where(array('order_id'=>$orderid))->save($data);
		if($result){
			$msg='物流信息填写成功';
		}else{
			$msg = '物流信息填写失败，联系管理人员';
		}
		$this->redirect('Membership/ordermanage',array('ostatusid'=>3,'bsflag'=>2,'msg'=>$msg));
	} 
	
	//买家取消订单
	function deleteOrder(){
		$uid = session('uid');
		$orderid = I('orderid');
		$dtype = I('dtype');
		$this->checkuid($uid,U('Order/deleteOrder',array('orderid'=>$orderid,'dtype'=>$dtype)));
		
		
		if(!$orderid){
			$msg = '订单操作错误，请联系管理人员';
			$this->redirect('Membership/ordermanage',array('ostatusid'=>1,'msg'=>$msg));
		}
		$orderDetail = M('order')->where(array('order_id'=>$orderid))->find();
		$result = M('order')->where(array('order_id'=>$orderid))->setField('order_status_id',4);//设置订单取消
		if($result){
			$result = M('products')->where(array('products_id'=>$orderDetail['products_id']))->setField('show_flag', 0);
			$data['order_id'] = $orderid;
			$data['delete_type'] = $dtype;
			$data['delete_date'] = date('Y-m-d H:i:s',time());
			M('deleteorder_log')->add($data); 
			$msg = '取消订单成功';			
			if($orderDetail['order_type']==0&&$orderDetail['order_status_id']==2){//线上交易，对于已经付款的用户，返还买家金额，扣减卖家金额，全部进入冻结状态
				//处理卖家退款				
				$result = D('MemberMoneylog')->Moneychange($orderDetail);				
				if(!$result){
					$msg = '退款失败，联系管理人员';
				}
			}
		}else{
			$msg = '取消订单失败，联系管理人员';
		}
		$this->redirect('Membership/ordermanage',array('ostatusid'=>0,'msg'=>$msg));
	}
	
	//交易申诉处理
	public function complain(){
		$uid = session('uid');
		$orderid = I('post.orderid');
		$this->checkuid($uid,U('Order/complain',array('orderid'=>$orderid)));
		
		if(!$orderid){
			$this->redirect('Membership/ordermanage',array('ostatusid'=>3));
		}
		$orderp = M('order')->where(array('order_id'=>$orderid))->field('products_id')->find();
		
		$data['order_id'] = $orderid;
		$data['products_id'] = $orderp['products_id'];
		$data['report_type'] = I('post.complaintype');
		$data['uid'] = $uid;
		$data['comment'] = I('post.complaintext');
		$data['report_date'] = date('Y-m-d H:i:s',time());
		$result = M('report')->add($data);
		if($result){
			$order_status_id=$data['report_type']==3?9:8;
			$order_status_id=8;
			$result = M('order')->where(array('order_id'=>$orderid))->setField('order_status_id',$order_status_id);
				//echo M('order')->getLastSql();die;
				//echo $result;die;
			if($result){				
				$msg = '交易申诉成功，请等待管理人员回复！';
			}else{
				$msg = '交易申诉失败，联系管理人员2';
			}
		
		}else{
			$msg = '交易申诉失败，联系管理人员1';
		}
		//echo $msg;die;
		$this->redirect('Membership/ordermanage',array('ostatusid'=>3,'msg'=>$msg));
	}
	/*======================================================
	 * 处理置顶或者加急收费
	 */
	public function fee(){
		$uid = session('uid');
		$pid = I('pid');
		//echo $pid;die;
		$feeType= I('feeType');
		$this->checkuid($uid,U('Order/fee',array('pid'=>$pid,'feeType'=>$feeType)));
		if($feeType=='up'){
			$fee = M('fee')->where(array('fee_id'=>1))->find();
		}elseif($feeType=='jiaji'){
			$fee = M('fee')->where(array('fee_id'=>2))->find();
		}else{
			$fee = M('fee')->where(array('fee_id'=>3))->find();
		}
		$data['fee_id'] = $fee['fee_id'];
		$data['fee_order_num'] = $fee_order_num = 'SF'.time();
		$data['fee_money'] = $fee['fee_money'];
		$data['products_id'] = $pid;
		$data['add_date'] = date('Y-m-d H:i:s',time());
		//$data['end_date'] = date('Y-m-d H:i:s',time()+$fee['active_day']*24*3600);
		//echo $data['end_date'];die;
		$data['uid'] = $uid;
		$fee_order_id = M('fee_order')->add($data);
		$products = M('products')->where(array('products_id'=>$pid))->field('products_id,products_name')->find();
		
		$this->feeOrderId = $fee_order_id;
		$this->fee = $fee;		
		$this->products = $products;
		$this->display();
	}
	
	//置顶收费确认反馈
	public function feeconfirm(){
		$uid = session('uid');
		$feeOrderId = I('feeOrderId');		
		$fee_id = I('fee_id');
		$this->checkuid($uid,U('Order/feeconfirm',array('feeOrderId'=>$feeOrderId,'fee_id'=>$fee_id)));
		
		$data['fee_order_id'] = $feeOrderId;
		$data['end_date'] = date('Y-m-d H:i:s',time()+$fee['active_day']*24*3600);
		//echo $data['end_date'];die;
		$data['fee_order_status'] = 1;
		$data['comment'] = I('comment');
		$result = M('fee_order')->save($data);
		if($result){//付款成功，保存商品置顶等信息
		/* 	if($fee_id==1){
				$data1['up_flag']=1;
			}elseif($fee_id==2){
				$data1['jiaji_flag']=1;
			}else{
				$data1['up_flag']=1;
				$data1['jiaji_flag']=1;
			}
			M('products')->where(array('products_id'=>I('products_id')))->save($data1); */
		}else{//失败
			echo 'fail';die;
		}
		$fee_order_num = M('fee_order')->where(array('fee_order_id'=>$feeOrderId))->field('fee_order_num')->find();
		$this->fee_order_num = $fee_order_num['fee_order_num'];
		$this->fee_order_id = $feeOrderId;
		$this->display();
	}
	
	//查看置顶收费订单详情
	public function feedetail(){		
		$uid = session('uid');
		$fee_order_id = I('feeorderid');
		$this->checkuid($uid,U('Order/feedetail',array('feeorderid'=>$fee_order_id)));
		
		$orderDetail = M('fee_order')->table('fee_order fo')->join('products p ON fo.products_id=p.products_id')->join('fee f ON fo.fee_id=f.fee_id')->where(array('fee_order_id'=>$fee_order_id))->field('fo.*,p.products_name,f.fee_name')->find();
		/* echo "<pre>";
		 var_dump($orderDetail);die; */
		$this->feeorderid = $fee_order_id;
		$this->orderDetail = $orderDetail;
		$this->display();
	}
	//卖家对订单评价
	public function sappraise(){
		$uid = session('uid');
		$orderid = I('orderid');
		$atype = I('atype');
		$this->checkuid($uid,U('Order/sappraise',array('orderid'=>$orderid,'atype'=>$atype)));
		
		$orderDetail = D('Order')->Orderdetail(array('order_id'=>$orderid));
		//var_dump($orderDetail);die;
		if(IS_POST){
			$score = I('post.score');
			$data['appraise_type'] = $atype;
			$data['uid'] = $orderDetail['uid'];
			$data['order_id'] = $orderid;
			$data['point'] = $score;
			$data['suid'] = $uid;
			$data['comment'] = I('post.comment');
			$data['appraise_date'] = date('Y-m-d H:i:s', time());
			$result = M('appraise')->add($data);
			if($result){
				$result1 = M('order')->where(array('order_id'=>$orderid))->setField('order_status_id', '7');
				
				if(!$result1){
					M('appraise')->where(array('appraise_id'=>$result))->delete();
					$msg = '评价失败';
				}else{
					$this->redirect('Membership/ordermanage',array('msg'=>'订单评价成功,订单成功!'));
				}
		
			}else{
				$msg = '评价失败';
			}
				
		}
		//获取卖家用户信息
		$showmember = M('membership')->where(array('uid'=>$orderDetail['publish_uid']))->field('name,tel')->find();
		$this->showmember = $showmember;
		$this->orderDetail = $orderDetail;
		$this->atype = $atype;
		$this->msg = $msg?$msg:0;
		$this->display('appraise');
	}
	
	//买家对卖家交易评价
	public function appraise(){
		$uid = session('uid');
		$orderid = I('orderid');
		$atype = I('atype');
		$this->checkuid($uid,U('Order/appraise',array('orderid'=>$orderid,'atype'=>$atype)));
		
		$orderDetail = D('Order')->Orderdetail(array('order_id'=>$orderid));
		//var_dump($orderDetail);die;
		if(IS_POST){
			$score = I('post.score');
			$data['appraise_type'] = $atype;
			$data['uid'] = $orderDetail['publish_uid'];
			$data['order_id'] = $orderid;
			$data['point'] = $score;
			$data['suid'] = $uid;
			$data['comment'] = I('post.comment');
			$data['appraise_date'] = date('Y-m-d H:i:s', time());
			
			$result = M('appraise')->add($data);
			if($result){
				
				if($orderDetail['order_type']==0){//若为线上交易
					//修改卖家冻结金额为解冻状态
					$result1 = D('MemberMoneylog')->changeFtoA($orderDetail['publish_uid'],$orderDetail['pay_price'],$orderid);
					
				}else{
					$result1 = M('order')->where(array('order_id'=>$orderid))->setField('order_status_id', '6');
				}
				if(!$result1){
					M('appraise')->where(array('appraise_id'=>$result))->delete();
					$msg = '评价失败';
				}else{
					$this->redirect('Membership/ordermanage',array('ostatusid'=>6,'msg'=>'订单确认及评价成功!'));
				}
				
			}else{
				$msg = '评价失败';
			}
			
		}
		//获取卖家用户信息
		$showmember = M('membership')->where(array('uid'=>$orderDetail['publish_uid']))->field('name,tel')->find();
		$this->showmember = $showmember;
		$this->orderDetail = $orderDetail;
		$this->atype = $atype;
		$this->msg = $msg?$msg:0;
		$this->display();
	}
	
}